package com.example.esp8266.Library.Data;

import androidx.annotation.NonNull;

public class SensorData {

    private double temperature;
    private double humidity;
    private int co2Concentration;
    private int pmIndex;
    private double lightIntensity;

    public SensorData(String rawData) {
        parseData(rawData);
    }

    private void parseData(String rawData) {
        if (rawData == null || rawData.isEmpty()) {
            throw new IllegalArgumentException("Raw data cannot be null or empty");
        }

        String[] dataSegments = rawData.split("_");
        for (String segment : dataSegments) {
            if (segment.startsWith("T.")) {
                temperature = Double.parseDouble(segment.substring(2));
            } else if (segment.startsWith("H.")) {
                humidity = Double.parseDouble(segment.substring(2));
            } else if (segment.startsWith("C.")) {
                co2Concentration = Integer.parseInt(segment.substring(2));
            } else if (segment.startsWith("P.")) {
                pmIndex = Integer.parseInt(segment.substring(2));
            } else if (segment.startsWith("L.")) {
                lightIntensity = Double.parseDouble(segment.substring(2));
            }
        }
    }

    public double getTemperature() {
        return temperature;
    }

    public double getHumidity() {
        return humidity;
    }

    public int getCo2Concentration() {
        return co2Concentration;
    }

    public int getPmIndex() {
        return pmIndex;
    }

    public double getLightIntensity() {
        return lightIntensity;
    }

    @NonNull
    @Override
    public String toString() {
        return "SensorData{" +
                "temperature=" + temperature +
                ", humidity=" + humidity +
                ", co2Concentration=" + co2Concentration +
                ", pmIndex=" + pmIndex +
                ", lightIntensity=" + lightIntensity +
                '}';
    }
}